package com.app.shortvideo.utils.jump

import android.app.Activity
import android.net.Uri
import android.text.TextUtils
import androidx.fragment.app.FragmentActivity

object JumpUtils {

    /*because of Can only use lower 16 bits for requestCode*/
    private const val NO_REQUEST_CODE = -0x1
    var instance: JumperFactory? = null

    fun setFactory(instance: JumperFactory?) {
        JumpUtils.instance = instance
    }

    fun handleJump(
        context: Activity,
        action: String?,
        decorator: IDecorateIntent?,
        requestCode: Int
    ): Boolean {
        if (instance == null) {
            throw NullPointerException("the Factory is not set,please call JumpUtils.setFactory to set a instance")
        }
        try {
            if (TextUtils.isEmpty(action)) {
                return false
            }
            val jumper = instance!!.createJumper(action)
            if (jumper != null) {
                val intent = jumper.generateIntent(context)
                if (!jumper.checkShouldJump(context)) {
                    jumper.onJumpInterrupted(context)
                    return false
                }
                if (intent == null) {
                    return false
                }
                decorator?.decorateIntent(intent)
                if (requestCode == NO_REQUEST_CODE) {
                    context.startActivity(intent)
                } else {
                    context.startActivityForResult(intent, requestCode)
                }
            }
        } catch (ex: Exception) {
            ex.printStackTrace()
            return false
        }
        return true
    }

    private fun jumpToTargetForResult(
        context: Activity,
        action: String?,
        decorator: IDecorateIntent?,
        requestCode: Int
    ): Boolean {
//        if (isNeedLogin(action) && !Session.isSessionOpend()) {
//            if (context instanceof FragmentActivity) {
//                SessionHelper.isSessionOpened((FragmentActivity) context, new SessionHelper.SessionCallback() {
//                    @Override
//                    public void sessionExpired() {
//
//                    }
//
//                    @Override
//                    public void sessionOpened() {
//                        handleJump(context, action, decorator, requestCode);
//                    }
//                });
//            }
//            return false;
//        } else {
        return handleJump(context, action, decorator, requestCode)
        //        }
    }

    fun jumpToTargetForResult(
        context: FragmentActivity,
        action: String?,
        requestCode: Int
    ): Boolean {
        return jumpToTargetForResult(context, action, null, requestCode)
    }

    @JvmOverloads
    fun jumpToTarget(
        context: Activity,
        action: String?,
        decorator: IDecorateIntent? = null
    ): Boolean {
        return jumpToTargetForResult(context, action, decorator, NO_REQUEST_CODE)
    }

    /**
     * 是否需要登录
     *
     * @param url
     * @return
     */
    fun isNeedLogin(url: String?): Boolean {
        if (TextUtils.isEmpty(url)) return false
        val uri = Uri.parse(url)
        val isLogin = uri.getQueryParameter("islogin")
        return isLogin != null && isLogin == "1"
    }

    /**
     * 获取Scheme 跳转URL
     * //pixgram://pixbe=nftDetail&author=p30104463&permlink=pixbe-p30104463-1628749073422
     *
     * @param parse
     * @return
     */
    fun getJumperUrl(parse: Uri): String {
        var result = StringBuilder()
        val parameters = parse.queryParameterNames
        if (parameters.contains("pixbe")) {
            val jumpStr = parse.getQueryParameter("pixbe")
            result = result.append("pixbe://").append(jumpStr).append("?")
            for (param in parameters) {
                if (param != "pixbe") {
                    result.append(param).append("=").append(parse.getQueryParameter(param))
                        .append("&")
                }
            }
        }
        return result.toString()
    }
}